package com.example.simpletodolist;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {
    public AlarmReceiver() {
    }

    @SuppressWarnings("deprecation")
	@Override
    public void onReceive(Context context, Intent intent) {
        if (intent !=null)
        {
        	Bundle b = intent.getExtras();	
            if (b !=null)
            {     
	            long rowid = b.getLong(ToDoListDatabase.KEY_ROWID);
	            
	            
				//TODO temporary logs to see what value of ROWID is
				rowid = b.getLong(ToDoListDatabase.KEY_ROWID);
				String tmptitle = b.getString(ToDoListDatabase.COL_TITLE);
				String tmpbody = b.getString(ToDoListDatabase.COL_BODY);
				String tmptime = b.getString(ToDoListDatabase.COL_TIME);
				String tmpdate = b.getString(ToDoListDatabase.COL_DATE);
				tempLog1(rowid, tmptitle, tmpbody, tmptime, tmpdate);
				///////////////////////////////////////////////////
            	Toast.makeText(context, "Receiver displaying alarm...", Toast.LENGTH_SHORT).show();
	            NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
	            int icon = R.drawable.ic_launcher;
	            CharSequence tickerText = "Time's up";
	            long when = System.currentTimeMillis();
	            
	            //this is depricated, but I want the app to function for  Froyo/GB
	            //the replacement Notification.Builder is not available for API 10 & lower
	            Notification notification = new Notification(icon, tickerText, when);
	            
	            String title = b.getString(ToDoListDatabase.COL_TITLE);
	            String body = b.getString(ToDoListDatabase.COL_BODY);
	            
	            Intent viewActivityIntent = new Intent(context, ToDoItemView.class);
	            viewActivityIntent.replaceExtras(b);
	            viewActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	            
	            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, viewActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
	            //this is depricated, but I want the app to function for  Froyo/GB
	            //the replacement Notification.Builder is not available for API 10 & lower
	            notification.setLatestEventInfo(context, title, body, contentIntent);
	            
	            notification.flags=Notification.FLAG_AUTO_CANCEL;
	            nm.notify(Integer.parseInt(rowid+""), notification);
	            

				//TODO temporary logs to see what value of ROWID is
	            b=viewActivityIntent.getExtras();
				Long tmp2rowid = b.getLong(ToDoListDatabase.KEY_ROWID);
				String tmp2title = b.getString(ToDoListDatabase.COL_TITLE);
				String tmp2body = b.getString(ToDoListDatabase.COL_BODY);
				String tmp2time = b.getString(ToDoListDatabase.COL_TIME);
				String tmp2date = b.getString(ToDoListDatabase.COL_DATE);
				tempLog2(tmp2rowid, tmp2title, tmp2body, tmp2time, tmp2date);
				///////////////////////////////////////////////////
	            // context.startActivity(newIntent);
	            Log.d("receiver", "activity started");
    		}
            else
            	throw new UnsupportedOperationException("Receiver received intent with no extras");
        }
        else
        	throw new UnsupportedOperationException("Receiver received null intent");
    }

	private void tempLog2(Long tmp2rowid, String tmp2title, String tmp2body,
			String tmp2time, String tmp2date) {
		Log.d("to view bundle",tmp2rowid + " rowid");
		Log.d("to view bundle",tmp2title + " title");
		Log.d("to view bundle",tmp2body + " body");
		Log.d("to view bundle",tmp2date + " date");
		Log.d("to view bundle",tmp2time + " time");
	}

	private void tempLog1(long rowid, String tmptitle, String tmpbody,
			String tmptime, String tmpdate) {
		Log.d("to receiver bundle",rowid + " rowid");
		Log.d("to receiver bundle",tmptitle + " title");
		Log.d("to receiver bundle",tmpbody + " body");
		Log.d("to receiver bundle",tmpdate + " date");
		Log.d("to receiver bundle",tmptime + " time");
	}
}
